/*************************
Diese Software ist ein Beispiel (sample code) und unterliegt der Microsoft Public License. 
Die Verwendung des Codes ist unter den Bedingungen der Microsoft Public License erlaubt.
*************************
This software is sample code and is subject to the Microsoft Public License. 
You may use this code according to the conditions of the Microsoft Public License.
*************************/
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Xml;
using System.Xml.Serialization;

namespace Egora.Stammportal.LdapAuthorizationService
{
  [Serializable()]
  public class ApplicationConfiguration
  {
    private PvpAttribute[] _pvpAttributesXml;

    [XmlElement("PvpAttribute")]
    public PvpAttribute[] PvpAttributes
    {
      get { return _pvpAttributesXml; }
      set { _pvpAttributesXml = value; }
    }

    private XmlAttribute[] _allAttributes;

    [XmlAnyAttribute]
    public XmlAttribute[] AllAttributes
    {
      get { return _allAttributes; }
      set { _allAttributes = value; }
    }

    private Dictionary<string, PvpAttribute> _pvpAttributes;

    public PvpAttribute GetPvpAttribute(string name)
    {
      if (_pvpAttributes == null)
      {
        _pvpAttributes = new Dictionary<string, PvpAttribute>();
        if (_pvpAttributesXml != null)
        {
          foreach (PvpAttribute attrXml in _pvpAttributesXml)
          {
            ApplicationConfiguration global = LdapConfiguration.GetConfiguration().GlobalApplication;
            if (this == global)
            {
              attrXml.GlobalAttribute = null;
            }
            else
            {
              attrXml.GlobalAttribute = global.GetPvpAttribute(attrXml.Name);
            }
            _pvpAttributes.Add(attrXml.Name, attrXml);
          }
        }
      }
      PvpAttribute attr;
      if (_pvpAttributes.TryGetValue(name, out attr))
        return attr;
      ApplicationConfiguration globalApp = LdapConfiguration.GetConfiguration().GlobalApplication;
      if (this != globalApp)
        return globalApp.GetPvpAttribute(name);
      return null;
    }

    public string GetConfigValue(string localName)
    {
      foreach (XmlAttribute attr in AllAttributes)
      {
        if (attr.LocalName == localName && !String.IsNullOrEmpty(attr.Value))
          return attr.Value;
      }
      return null;
    }

    public string GetConfigValueWithGlobal(string localName)
    {
      string localValue = GetConfigValue(localName);
      if (localValue == null)
      {
        ApplicationConfiguration global = LdapConfiguration.GetConfiguration().GlobalApplication;
        if (this != global)
          return global.GetConfigValue(localName);
      }
      return localValue;
    }

    public string[] GetAttributeValue(string pvpAttribute, DirectoryEntry user, List<DirectoryEntry> groups)
    {
      PvpAttribute attr = GetPvpAttribute(pvpAttribute);
      if (attr != null)
        return attr.GetValue(user, groups);
      return null;
    }

    [XmlIgnore]
    public string Name
    {
      get { return GetConfigValue("name"); }
    }

    [XmlIgnore]
    public string DomainPrefix
    {
      get { return GetConfigValueWithGlobal("domainPrefix"); }
    }

    [XmlIgnore]
    public string LdapRoot
    {
      get { return GetConfigValueWithGlobal("ldapRoot"); }
    }

    [XmlIgnore]
    public string UserProperties
    {
      get { return GetConfigValue("userProperties"); }
    }

    [XmlIgnore]
    public int AuthorizationTimeToLive
    {
      get { return Convert.ToInt32(GetConfigValueWithGlobal("authorizationTimeToLive")); }
    }

    [XmlIgnore]
    public string SoapPrincipalFragment
    {
      get { return GetConfigValueWithGlobal("soapPrincipal"); }
    }

    [XmlIgnore]
    public string WebUrls
    {
      get { return GetConfigValue("webUrls"); }
    }

    [XmlIgnore]
    public string SoapUrls
    {
      get { return GetConfigValue("soapUrls"); }
    }

    [XmlIgnore]
    public string GroupContainer
    {
      get { return GetConfigValue("groupContainer"); }
    }

    [XmlIgnore]
    public bool RecurseGroupMembership
    {
      get { return GetConfigValue("recurseGroupMembership") == "true"; }
    }

    [XmlIgnore]
    public bool MustHaveRole
    {
      get { return GetConfigValue("mustHaveRole") == "true"; }
    }

    public bool IsWeb(string rootUrl)
    {
      return (WebUrls != null && WebUrls.Contains(rootUrl));
    }

    public bool IsSoap(string rootUrl)
    {
      return (SoapUrls != null && SoapUrls.Contains(rootUrl));
    }
  }
}